<template>
    住院房间2管理
    <!-- 搜索表 -->
    <el-form :inline="true" :model="formInline" class="w-full h-10">
        <el-form-item label="名称">
            <el-input v-model="formInline.roomName" placeholder="姓名" />
        </el-form-item>
        <el-form-item label="患者姓名">
            <el-input v-model="formInline.patientname" placeholder="楼层" />
        </el-form-item> 
        <el-form-item label="医生电话">
            <el-input v-model="formInline.phonenumber" placeholder="楼层" />
        </el-form-item>        
        <el-form-item>
            <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
    </el-form>
    <el-button type="danger" @click="delMore">批量删除</el-button>
    <div class="w-full">
        <!-- 表格显示 -->
        <el-table :data="tableData" style="width: 100%" border height="60vh" highlight-current-row @row-dblclick="getInfo" @selection-change="selected">
            <el-table-column fixed type="selection" width="50" />
            <el-table-column prop="inhospitalId" label="序号" width="80" align="center"></el-table-column>
            <el-table-column prop="roomName" label="房间名称"  align="center"></el-table-column>
            <el-table-column prop="doctorname" label="医生"  align="center"></el-table-column>
            <el-table-column prop="patientname" label="患者名称"  align="center"></el-table-column>
            <el-table-column prop="location" label="位置"  align="center"></el-table-column>
            <el-table-column prop="status" label="缴纳押金"  align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.status == 1" type="success">已缴纳</el-tag>
                    <el-tag v-else type="danger">未缴纳</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="method" label="缴费方式"  align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.status == 0" type="danger">-</el-tag>
                    <el-tag v-else-if="scope.row.method == 1" type="success">线上</el-tag>
                    <el-tag v-else type="primary">现金</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="deposit" label="押金"  align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"  align="center"></el-table-column>
            <el-table-column prop="creater" label="创建人"  align="center"></el-table-column>
            <el-table-column prop="payTime" label="缴费时间"  align="center"></el-table-column>
            <el-table-column prop="haveOut" label="是否出院"  align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.haveOut == 1" type="success">已出院</el-tag>
                    <el-tag v-else type="danger">未出院</el-tag>
                </template>
            </el-table-column>
            
            <el-table-column label="操作"  align="center" width="200">
                <template #default="scope">
                    <el-button type="primary" @click="addOne(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <!-- 页码 -->
    <div class="flex justify-center items-end w-full h-16 mt-2">
        <el-pagination
            v-model:current-page="pageForm.currentPage"
            v-model:page-size="pageForm.pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageForm.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            />
            <el-switch v-model="background" class="ml-2" />
    </div>
    <!-- 进行编辑添加 -->
    <el-dialog v-model="dialogFormVisible" title="编辑信息" width="300">
        <el-form :model="addEditForm" label-width="80px">
            <el-form-item label="名称">
                <el-input v-model="addEditForm.roomName" placeholder="名称" />
            </el-form-item>
            <el-form-item label="楼层">
                <el-input v-model="addEditForm.floor" placeholder="楼层" />
            </el-form-item>
            <el-form-item label="科室">
                <el-input v-model="addEditForm.department" placeholder="科室" />
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="addEditForm.status" placeholder="状态">
                    <el-option label="启用" value="1" />
                    <el-option label="禁用" value="2" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addEdit">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!-- 进行查看详情 -->
    <el-dialog v-model="dialogInfoVisible" title="用户详情" width="300">
        <el-form :model="addEditForm" label-width="80px">
            <el-form-item label="名称">
                <el-input v-model="addEditForm.roomName" placeholder="名称" />
            </el-form-item>
            <el-form-item label="楼层">
                <el-input v-model="addEditForm.floor" placeholder="楼层" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addEdit">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

</template>

<script setup>
import { ref, reactive, computed} from "vue";
import { ElMessageBox, ElMessage } from 'element-plus'
import { getList } from "@/api/user/roominfo.js"
// 表数据区
let tableData = reactive([
    { id: 1, roomName : "adad", doctorName : "adad", patientName : "adad", location : "adad", status : 1, method : 1, deposit : 100, createTime : "2022-01-01", creater : "admin", havePay : 1, payTime : "2022-01-01" },
    { id: 2, roomName : "adad", doctorName : "adad", patientName : "adad", location : "adad", status : 2, method : 2, deposit : 200, createTime : "2022-01-01", creater : "admin", havePay : 2, payTime : "2022-01-01" },
    { id: 3, roomName : "adad", doctorName : "adad", patientName : "adad", location : "adad", status : 1, method : 1, deposit : 300, createTime : "2022-01-01", creater : "admin", havePay : 1, payTime : "2022-01-01" },
    { id: 4, roomName : "adad", doctorName : "adad", patientName : "adad", location : "adads", status : 2, method : 2, deposit : 400, createTime : "2022-01-01", creater : "admin", havePay : 2, payTime : "2022-01-01" }
])
// 添加编辑
const dialogFormVisible = ref(false)
// 详情
const dialogInfoVisible = ref(false)
// 搜索表
const formInline = reactive({
    roomName: "",
    patientname: "",
    phonenumber :""
})
// 页数
const pageForm = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 50
})
// 背景
const background = ref(false)
// 搜索条件
const pageSearch = computed(() => {
  return {
    page: pageForm.currentPage,
    limit: pageForm.pageSize,
    roomName: formInline.roomName,
    patientname: formInline.patientname,
    phonenumber: formInline.phonenumber,
  }
})
// 编辑或者添加
var addEditForm = reactive({roomName: "", floor: "", department: "", status: 1})
// 选择复选框
let idArr = []

// 方法
initData()

function initData(){
    getList(pageSearch.value).then(res => {
        console.log(res)
        tableData = res.data.list
        pageForm.currentPage = res.data.currPage
        pageForm.total = res.data.totalCount
        pageForm.pageSize = res.data.pageSize
    })
}

const selected = (data) => {
  idArr = []
  data.forEach((value) => {
    idArr.push(value)
  })
  console.log(data)
  console.log(idArr)
}

// 点击搜索
const onSearch = () => {
  console.log(pageSearch)
  initData()
}
// 获取详细信息
function getInfo(row){
    dialogInfoVisible.value = true
    console.log(row)
}

const handleSizeChange = (val) => {
//   console.log(`${val} items per page`)
    initData()
}
const handleCurrentChange = (val) => {
//   console.log(`current page: ${val}`)
    initData()
}
// 删除
function delMore(){
    if(idArr.length == 0){
        ElMessage.error("请选择")
        return
    }
    alert(idArr.length)
    // 进行删除
    ElMessage.success("删除成功")
}
// 进行添加
function addOne(n){
    dialogFormVisible.value = true
    console.log("进行编辑")
    addEditForm = computed(()=>{
        return {
            roomName: n.roomName,
            floor: n.floor,
            department: n.department,
            status: n.status
        }
    })
    
}

// 确定
function addEdit(){
    dialogFormVisible.value = false
    console.log("点击了确定")
    console.log(addEditForm)
    ElMessage.success("添加成功")
}
</script>

<style scoped>

</style>